//
//  User.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/6.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "User.h"
#import "YFUserDefaults.h"
#import "DeviceInfo.h"
#import "TrackVideo.h"
#import "NSString+Extension.h"

@implementation User

+(NSString *)primaryKey{
    return @"identification";
}
///获取用户
+ (User *)userWithPrivateKey:(NSString *)key
{
    User *user = [User objectForPrimaryKey:key];
    if (user == nil) {
        user = [[User alloc] init];
        user.identification = key;
        user.userID = key;
        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm beginWriteTransaction];
        [realm addObject:user];
        [realm commitWriteTransaction];
    }
    return user;
}

///当前用户
+ (User *)getLastUser{
    NSString *userID = [YFUserDefaults lastLoginID];
    if (userID.length == 0) {
        return nil;
    }
    User *user = [User userWithPrivateKey:userID];
    return user;
}

+ (UserInfoItem *)getUseInfo {
    return [UserInfoItem userInfoWithPrivateKey:[User getLastUser].userID];
}

/**
 * 获取账户类型,用于数据同步
 */
- (UserType)userType {
    UserType type;
    DeviceInfo *device = [DeviceInfo getLastDevice];
    User *user = [User getLastUser];
    BOOL isHavePoint = YES;
    BOOL isHaveMac = ![DeviceInfo getLastDevice].isNewChangeConnect;
    BOOL isHaveData = YES;
    if (user.syncDataTimeStamp.length <= 0 || ![device.syncSwingDataTimeStamp isEqualToString:user.syncDataTimeStamp]) {
        isHavePoint = NO;
    }
    if (isHaveMac == NO && isHavePoint == NO && isHaveData == NO) {
        type = UserTypeNewUserNewApp;
    }else if (isHaveMac == NO && isHavePoint == NO && isHaveData == YES){
        type = UserTypeOldUserNewApp;
    }else if (isHaveMac == YES && isHavePoint == YES && isHaveData == YES){
        type = UserTypeNormal;
    }else{
        type = UserTypeError;
    }
    return type;
}

#pragma mark - 处理系统模板
- (void)checkAndCreatSystemTemplate {
    if ([YFThreeDTemplate getCurrentUserAllSystemTemplateModels].count == 0) {
        dispatch_async(dispatch_queue_create("creatSystemTemplate", DISPATCH_QUEUE_SERIAL), ^{
            //            NSString *systemTemPath = [[NSBundle mainBundle] pathForResource:@"SystemTemplate.plist" ofType:nil];
            //             NSArray *array = [NSArray arrayWithContentsOfFile:systemTemPath];
            NSString *path = [[NSBundle mainBundle] pathForResource:@"SystemModel.txt" ofType:nil];
            NSString *threeDJsonString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
            NSArray *array = [NSString dictionaryWithJsonString:threeDJsonString];
            
            NSArray *imageNames = @[@"吊球",@"挑球",@"长球",@"侧墙球",@"其他"];
            NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:array.count];
            [array enumerateObjectsUsingBlock:^(NSDictionary *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                YFThreeDItem *threeDItem = [[YFThreeDItem alloc] init];
                [threeDItem parseThreeDItemFormDeviceDict:obj];
                for (NSDictionary *detailDict in obj[@"threeDDetailItems"]) {
                    YFThreeDDetailItem *detailItem = [[YFThreeDDetailItem alloc] init];
                    [detailItem parseThreeDDetailItemFormDeviceDict:detailDict];
                    [threeDItem.threeDDetailItems addObject:detailItem];
                }
                // 创建系统模板
                YFThreeDTemplate *systemTemplate = [[YFThreeDTemplate alloc] initWithThreeDitem:threeDItem name:[NSString stringWithFormat:@"Demo Action Model - %zd",idx + 1] templateImage:imageNames[idx % 5] mainType:TemplateMainTypeSystem];
                systemTemplate.handType = HandDirectionTypeForeward;
                systemTemplate.selectActionType = idx;
                [arrM addObject:systemTemplate];
            }];
            dispatch_async(dispatch_get_main_queue(), ^{
                [[RLMRealm defaultRealm] beginWriteTransaction];
                [[RLMRealm defaultRealm] addOrUpdateObjects:arrM.copy];
                if (self.currentSelectTemplate == nil) {
                    self.currentSelectTemplate = [YFThreeDTemplate getCurrentUserAllSystemTemplateModels].firstObject;
                }
                [[RLMRealm defaultRealm] commitWriteTransaction];
            });
        });
    }
}

@end
